package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingChartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    //查询购物车
    @GetMapping("/list")
    public R list(){
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartService.list(lambdaQueryWrapper);
        lambdaQueryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        return R.success(list);
    }

    //添加购物车
    @PostMapping("/add")
    public R addShoppingCart(@RequestBody ShoppingCart shoppingCart){
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);

        // 判断当前用户点的是菜品还是套餐
        if (dishId != null){
            //不为空时添加的为菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //为空时添加的为套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);

        // 第一次点的时候是新增 ，后面在点击的时候是修改
        if (cart !=  null){
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartService.updateById(cart);
            return R.success(cart);
        }else{
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }
    }

    //减少购物车
    @PostMapping("/sub")
    public R subShoppingCart(@RequestBody ShoppingCart shoppingCart){

        //菜品或者套餐id 查询出当前ShoppingCart数据
        //如何是菜品：select * from shopping_cart where dish_id = ? and user_id = ?
        //如果是套餐：select * from shopping_cart where setmeal_id = ? and user_id = ?
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        Long dishId = shoppingCart.getDishId();
        if (dishId == null){
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        } else {
            //减少的是菜品的数量
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        Integer number = one.getNumber();

        if (number > 1){
            // >1  update更新 update set number=? where id = ?
            one.setNumber(one.getNumber() - 1);
            shoppingCartService.updateById(one);
        }else {
            // <= 1  如何操作？ delete from shopping_cart where dish_id=? and user_id = ?
            shoppingCartService.removeById(one.getId());
        }
        return R.success(one);
    }

    //清空购物车
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("删除成功");
    }
}
